﻿using System.Collections.Generic;
using Apache.Cassandra;

namespace SKBKontur.Cassandra.Proxy.Helpers
{
    public static class ProxyExtensions
    {
        public static Dictionary<byte[], List<ColumnOrSuperColumn>> MultigetSliceFromColumnFamily(this IStorage storage,
                                                                                                  IEnumerable<byte[]>
                                                                                                      keys,
                                                                                                  string columnFamily,
                                                                                                  byte[] minColumnName,
                                                                                                  byte[] maxColumnName,
                                                                                                  int count,
                                                                                                  ConsistencyLevel
                                                                                                      consistencyLevel)
        {
            var slicePredicate = new SlicePredicate
                                     {
                                         Slice_range = new SliceRange
                                                           {
                                                               Start = minColumnName,
                                                               Finish = maxColumnName,
                                                               Count = count,
                                                               Reversed = false
                                                           }
                                     };
            Dictionary<byte[], List<ColumnOrSuperColumn>> returned = storage.MultigetSlice(keys, new ColumnParent
                                                                                                     {
                                                                                                         Column_family =
                                                                                                             columnFamily
                                                                                                     }, slicePredicate,
                                                                                           consistencyLevel);
            return returned;
        }

        public static List<ColumnOrSuperColumn> GetSliceFromColumnFamily(this IStorage storage, byte[] key,
                                                                         string columnFamily, byte[] minColumnName,
                                                                         byte[] maxColumnName, int count,
                                                                         ConsistencyLevel consistencyLevel)
        {
            var slicePredicate = new SlicePredicate
                                     {
                                         Slice_range = new SliceRange
                                                           {
                                                               Start = minColumnName,
                                                               Finish = maxColumnName,
                                                               Count = count,
                                                               Reversed = false,
                                                           }
                                     };
            return storage.GetSlice(key, new ColumnParent
                                             {
                                                 Column_family = columnFamily,
                                             }, slicePredicate, consistencyLevel);
        }

        public static List<KeySlice> GetKeyIndexedSliceFromColumnFamily(this IStorage storage, string columnFamily, List<IndexExpression> indexExpressions,
             int count, ConsistencyLevel consistencyLevel)
        {
            var columnParent = new ColumnParent
                                   {
                                       Column_family = columnFamily,
                                   };
            var indexClause = new IndexClause()
                                  {
                                      Count = count,
                                      Start_key = new byte[0],
                                      Expressions = indexExpressions,
                                                        
                                  };
            var slicePredicate = new SlicePredicate
                                     {
                                         Slice_range = new SliceRange
                                                           {
                                                               Start = new byte[0],
                                                               Finish = new byte[0],
                                                               Count = int.MaxValue,
                                                               Reversed = false
                                                           }
                                     };

            return storage.GetIndexedSlice(columnParent, indexClause, slicePredicate, consistencyLevel);
        }
    }
}